<?php
session_start();
@header('Content-type: text/html;charset=UTF-8');
include '../include/Mysql.Class.php';
include '../include/config.php';
include 'sendData.php';
$userIdtxt = $_SESSION['userId'];//企业编码
$companyname = $_SESSION['companyname'];//企业名称
$codetxt = $_SESSION['code'];//站点编码
$nametxt = $_SESSION['name'];//站点名称
$countyTypetxt = $_SESSION['countyType'];//站点类型
$countyTypeName = $_SESSION['countyTypeName'];//站点类型名称
$buyIds = $_POST[buyId];//购买商品编号
$buyNames = $_POST[buyName];//购买商品名称
$buys = $_POST[buy];//购买商品金额
$sellIds = $_POST[sellId];//销售商品编号
$sellNames = $_POST[sellName];//销售商品名称
$sells = $_POST[sell];//销售商品金额
$buyOrdertxt = 0;
$saleOrdertxt = 0;
$Commodity = array();
//插入上报记录表
$temp = 0;
$insertTabel = array();
for($i=0;$i<count($buys);$i++){
    if (!empty($buys[$i])){
        $Commodity[$buyIds[$i]] = $buys[$i];
        $buyOrdertxt++;
        $insertTabel[$temp] = "'$userIdtxt','$companyname','$codetxt','$nametxt','$countyTypetxt','$countyTypeName','$buyIds[$i]','$buyNames[$i]','$buys[$i]','B',year(now()),month(now()),day(now()),now()";
        $temp++;
    }
}
for($i=0;$i<count($sells);$i++){
    if (!empty($sells[$i])){
        $Commodity[$sellIds[$i]] = $sells[$i];
        $saleOrdertxt++;
        $insertTabel[$temp] = "'$userIdtxt','$companyname','$codetxt','$nametxt','$countyTypetxt','$countyTypeName','$sellIds[$i]','$sellNames[$i]','$sells[$i]','S',year(now()),month(now()),day(now()),now()";
        $temp++;
    }
}
$mysql = new mysql($mydbhost, $mydbuser, $mydbpw, $mydbname, $conn, $mydbcharset);
$sql = "select * from uploadlog where code='$codetxt' and upYear=year(now()) and upMonth=month(now()) and upDay=day(now())";
$mysql->query($sql);
if($row = $mysql->fetch_assoc()){
    echo "<script language='javascript'>alert('数据已经上报过，不能重复上报!');history.back();</script>";
}else{
    $xmlData = constructXmlData($userIdtxt, $codetxt, $nametxt, $countyTypetxt, $buyOrdertxt, $saleOrdertxt,$Commodity);
    $responseXmlStr = uploadData($xmlData);
    if (empty($responseXmlStr)){
        echo "<script language='javascript'>alert('上报失败!');history.back();</script>";
    }else{
        $columnName = "userId,userName,code,name,countyType,countyTypeName,goodCode,goodName,moneyCount,goodType,upYear,upMonth,upDay,uploadDate";
        $columnName1 = "userId,userName,code,name,countyType,countyTypeName,upYear,upMonth,upDay,uploadDate,uploadType";
        $insertTabel1 = "'$userIdtxt','$companyname','$codetxt','$nametxt','$countyTypetxt','$countyTypeName',year(now()),month(now()),day(now()),now(),1";
        $mysql->insert("uploadlog", $columnName1, $insertTabel1);
        for($i=0;$i<count($insertTabel);$i++){
            if (!empty($insertTabel[$i])){
                $mysql->insert("reportrecord", $columnName, $insertTabel[$i]);
                //echo $columnName."-------".$insertTabel[$i]."<br/>";
            }
        }
        echo "<script language='javascript'>alert('上报成功!');window.location.href='uploadInfo.php';</script>";
    }
}


